//
//  ZLEmotionTapView.swift
//  ZLWeibo
//
//  Created by zhanglongtao on 17/1/19.
//  Copyright © 2017年 hanju001. All rights reserved.
//

import UIKit
import pop

//表情选择提示视图
class ZLEmotionTapView: UIImageView {

    //之前选择的表情
    private var preEmotion: ZLEmotion?
    
    var emotion: ZLEmotion? {
        didSet {
            
            if emotion == preEmotion{
                
                return
            }
            
            //记录之前的表情
            preEmotion = emotion
            
            //设置表情数据
            tipButton.setImage(emotion?.image, for: [])
            //设置emoji的字符串  这样写自带清空功能
            
            tipButton.setTitle(emotion?.emoji, for: [])
            //表情动画- 弹力动画的结束时间是根据速度自动计算的，不需要也不能指定duration
            let animation: POPSpringAnimation = POPSpringAnimation(propertyNamed: kPOPLayerPosition)
            animation.fromValue = 30
            animation.toValue = 8
            
            animation.springBounciness = 20
            animation.springSpeed = 20
            
            tipButton.layer.pop_add(animation, forKey: nil)
            
            
        }
    }
    // MARK - 私有控件
    private lazy var tipButton = UIButton()
    
    // MARK - 构造函数
    init() {
        let bundle = EmotionManager.shared.bundle
        let image = UIImage(named: "emoticon_keyboard_magnifier", in: bundle, compatibleWith: nil)
        //会根据图片大小设置imageView的大小
        super.init(image: image)
        
        //设置锚点
        layer.anchorPoint = CGPoint(x: 0.5, y: 1.2)
        
        //添加按钮
        tipButton.layer.anchorPoint = CGPoint(x: 0, y: 0)
        tipButton.frame = CGRect(x: bounds.width * 0.5, y: 8, width: 36, height: 36)
        tipButton.center.x = bounds.width * 0.5
        
        tipButton.setTitle("😄", for: [])
        tipButton.titleLabel?.font = UIFont.systemFont(ofSize: 32)
        
        addSubview(tipButton)
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
